﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using SO_DataFetcher.PubFunctions.Paraments;

namespace SO_DataFetcher.PubFunctions.SqlScripts
{
     public class PubScripts
    {

        public static Dictionary<string, ScriptStruct> Scripts = new Dictionary<string, ScriptStruct>();    //公共字典集
        public static DataSet DatasetScripts = null;    //公共数据集     
        public static DataTable DataTableScripts = new DataTable();
        private static readonly string XmlPath = PubCommon.XmlSciptsPath;
        private static bool _isXmlExist = false;

        /// <summary>
        /// 初始化脚本
        /// </summary>
        public static void Init()
        {
            try
            {
                IsXmlExist();

                if (_isXmlExist)
                    GetTableScripts();
                else
                {
                    DataTableScripts = new DataTable();
                    DataTableScripts.Columns.Add("Index", typeof (string));
                    DataTableScripts.Columns.Add("Name", typeof(string));
                    DataTableScripts.Columns.Add("File", typeof(string));
                    DataTableScripts.Columns.Add("subParament", typeof(string));
                }

                PubLog.WriteLog("初始化脚本配置文件完成");
            }
            catch (Exception ex)
            {
                PubLog.WriteLog("初始化脚本配置文件错误："+ ex.Message);
            }
        }

        public static void IsXmlExist()
        {
            _isXmlExist = File.Exists(XmlPath);
        }

        /// <summary>                                                        
        /// 更新连接数据
        /// </summary>                                                     
        private static void GetTableScripts()      
        {
            DatasetScripts = PubXml.GetDataSetFromXml(XmlPath);

            if (DatasetScripts.Tables.Count > 0)
            {
                DataTableScripts = DatasetScripts.Tables[0];

                if (DataTableScripts != null && DataTableScripts.Rows.Count > 0)
                    GetDictionaryScripts();
            }
        }

 
         /// <summary>
         /// 存储为字典变量
         /// </summary>
         private static void GetDictionaryScripts()
        {
            Scripts = new Dictionary<string, ScriptStruct>();

            foreach (DataRow dataRow in DataTableScripts.Rows)
            {
                string index = dataRow["Index"].ToString();
                string name = dataRow["Name"].ToString();
                string file = dataRow["File"].ToString();
                string paraments = dataRow["subParament"].ToString();

                var scriptStruct = new ScriptStruct()
                {
                  Index = index,
                  Name = name,
                  File = file,
                  Paraments = paraments
                };

                Scripts.Add(name, scriptStruct);
            }
        }


        /// <summary>
        /// 保存修改后的dataset到xml文件中
        /// </summary>
        public static void SaveDataSetLinks()
        {
            if (DatasetScripts != null)
            {
                DatasetScripts.Tables.Clear();
                DatasetScripts.Tables.Add(DataTableScripts);
                GetDictionaryScripts();
                XmlUtility.SaveXmlSetting(DatasetScripts, XmlPath);
                IsXmlExist();
            }
        }


         /// <summary>
         /// 通过对应sqlScript的id获取对应的sql语句
         /// </summary>
         /// <param name="ids"></param>
         /// <returns></returns>
        public static string[] SetSqlCurrentScripts(string[] ids)
         {
            List<string> listSqls = new List<string>();
            foreach (string id in ids)
            {
                if (Scripts.ContainsKey(id))
                {
                    ScriptStruct scriptStruct = Scripts[id];

                    string[] paraments = PubParament.GetParamentsValue(scriptStruct.Paraments.Split(','));

                    string fileNmae = PubCommon.MainPath + @"\SqlScripts\" + scriptStruct.File;

                    if (File.Exists(fileNmae))
                    {
                        string sql = string.Format(PubCommon.GetFileText(fileNmae), (object[])paraments);
                        listSqls.Add(sql);
                    }
                }
            }

            return listSqls.ToArray();
         }
    }
}
